#include "common.h"
#include "syscall.h"
#include "fs.h"
#include "proc.h"

uintptr_t do_sysbrk(uintptr_t brk,uintptr_t incr);
uintptr_t do_execute(const char *fname, char * const argv[], char *const envp[]);
void naive_uload(PCB *pcb, const char *filename);

_Context* do_syscall(_Context *c) {
  uintptr_t a[4];
  a[0] = c->GPR1;
  a[1] = c->GPR2;
  a[2] = c->GPR3;
  a[3] = c->GPR4;

  switch (a[0]) {
    case SYS_yield : _yield(); break;
    case SYS_exit : printk("exit(%d)\n",a[1]);_halt(a[1]); break;
    case SYS_brk : c->GPRx = do_sysbrk(a[1],a[2]); break;
    case SYS_open : c->GPRx = fs_open((char*)a[1],a[2],a[3]);break;
    case SYS_write : c->GPRx = fs_write(a[1],(void *)a[2],a[3]); break;
    case SYS_read : c->GPRx = fs_read(a[1],(void *)a[2],a[3]);break;
    case SYS_lseek : c->GPRx = fs_lseek(a[1],a[2],a[3]);break;
    case SYS_close : c->GPRx = fs_close(a[1]);break;
    case SYS_execve : c->GPRx = do_execute((char *)a[1],(void *)a[2],(void *)a[3]);break;
    default: panic("Unhandled syscall ID = %d", a[0]);
  }

  return c;
}

uintptr_t do_execute(const char *fname, char * const argv[], char *const envp[]) {
  printk("call execute\n");
  naive_uload(NULL, fname);
  return 0;
}


uintptr_t do_sysbrk(uintptr_t brk,uintptr_t incr) {
  printk("call sysbrk\n");
  return 0;
}
